package com.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by wgy on 2017-03-06.
 * 序列化工具类<br>
 * 使用JDK自带方式进行序列化和反序列化
 */
public class SerializeUtil {

    /**
     *
     * 功能描述: <br>
     * 序列化 成byte数组
     */
    public static byte[] serialize(Object object) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        return baos.toByteArray();
    }

    /**
     *
     * 功能描述: <br>
     * 反序列化
     */
    public static Object unserialize(byte[] byteArray) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
        ObjectInputStream ois = new ObjectInputStream(bais);
        return ois.readObject();
    }
}